//
//  AddBankAccountTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/21.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "AddBankAccountTableViewController.h"
#import "ChooseView.h"
#import "SelectCountryTableViewController.h"
#import "HYNetHelper.h"

@interface AddBankAccountTableViewController ()

@property (strong, nonatomic) NSArray *accountTypeArray;
@property (strong, nonatomic) ChooseView *chooseView;
@property (weak, nonatomic) IBOutlet UIView *chooseBgView;
@property (nonatomic, strong) CountryEntity *countryEntity;

@property (weak, nonatomic) IBOutlet UITextField *country;
@property (weak, nonatomic) IBOutlet UITextField *holderName;
@property (weak, nonatomic) IBOutlet UITextField *accountNumber;
@property (weak, nonatomic) IBOutlet UITextField *bankName;
@property (weak, nonatomic) IBOutlet UITextView *bankDetails;

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *bankTypeTitleLable;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *countryTitleLable;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *accountHolderNameTitleLable;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *AccountNumberTitleLable;

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *bankNameTitleLable;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *bankDetailTitleLable;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation AddBankAccountTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = JFMainLocalizedString(@"bank.add-bank");//@"Add Bank Account";
    self.accountTypeArray = @[JFMainLocalizedString(@"bank.personal"), JFMainLocalizedString(@"bank.corporate")];
    
    
    self.bankTypeTitleLable.text = JFMainLocalizedString(@"bank.account-type");
    self.countryTitleLable.text = JFMainLocalizedString(@"bank.country");
    self.accountHolderNameTitleLable.text = JFMainLocalizedString(@"bank.holderName");
    self.AccountNumberTitleLable.text = JFMainLocalizedString(@"bank.account-number");
    self.bankNameTitleLable.text = JFMainLocalizedString(@"bank.bank-name");
    self.bankDetailTitleLable.text = JFMainLocalizedString(@"bank.details");
    
    self.confirmBtn.AWtitle(JFMainLocalizedString(@"confirm"), UIControlStateNormal);
    
    self.country.AWplaceholder(JFMainLocalizedString(@"bank.country"));
     self.holderName.AWplaceholder(JFMainLocalizedString(@"bank.holderName"));
     self.accountNumber.AWplaceholder(JFMainLocalizedString(@"bank.account-number"));
     self.bankName.AWplaceholder(JFMainLocalizedString(@"bank.bank-name"));
    
    
    
    
    WEAKSELF
    self.chooseView = [[ChooseView alloc]initWithFrame:CGRectMake(15, 55, SCREEN_WIDTH - 30, 15) itemArray:@[JFMainLocalizedString(@"bank.personal"), JFMainLocalizedString(@"bank.corporate")]];
    self.chooseView.vauleChange = ^(NSInteger selectIndex) {
        [weakSelf.tableView reloadData];
    };
    [self.chooseBgView addSubview:self.chooseView];
}

#pragma mark - Table view data source

- (IBAction)submitAction:(id)sender {
    NSString *account_type = [self.accountTypeArray[self.chooseView.selectIndex] uppercaseString];
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    [array addObject:@{@"account_type" : account_type}];
    [array addObject:@{@"country" : self.countryEntity.countryId}];
    [array addObject:@{@"account_name" : self.holderName.text}];
    [array addObject:@{@"account_number" : self.accountNumber.text}];
    [array addObject:@{@"bank_name" : self.bankName.text}];
    if (self.bankDetails.text.length) {
        [array addObject:@{@"bank_detail" : self.bankDetails.text}];
    }
    WEAKSELF
    [HYNetHelper bankAccount_submit:@"0" user_remark:@"" account_type:account_type fields:array callback:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon1"] title:@"Add Requested" customView:nil message:@"Add Bank Account Success"];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
                                }];
            [alertController addAction:action];
            [weakSelf presentViewController:alertController animated:YES completion:nil];
        }
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        SelectCountryTableViewController *vc = [[SelectCountryTableViewController alloc]init];
        WEAKSELF
        vc.selectAction = ^(CountryEntity * _Nonnull country) {
            weakSelf.countryEntity = country;
            weakSelf.country.text = country.countryName;
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
